﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace WeChatDataSrc
{
    /// <summary>
    /// 通过Windows共享的方式登录远程主机
    /// </summary>
   public class RemoteHost
    {
       string standardError;
       string standardOutput;
       string userName;
       string passWord;
       string hostName;

       //创建一个进程类的实例
       Process p = new Process();
       
       /// <summary>
       /// 
       /// </summary>
       /// <param name="HostName">远程主机地址</param>
       /// <param name="User">登录账户</param>
       /// <param name="Pwd">登录密码</param>
       public RemoteHost(string HostName,string User,string Pwd)
       {
           hostName = HostName;
           foreach (string str in hostName.Split('\\'))
           {
               if (str != "" && str != "\\" && str.Contains("."))
               {
                   hostName = str;
                   break;
               }
           }

           userName = User;
           passWord = Pwd;
         
          
           //Process类有一个StartInfo属性，
           //这个是ProcessStartInfo类，包括了一些属性和方法，
           //下面我们用到了他的几个属性： 
           p.StartInfo.FileName = "cmd.exe ";              //设定程序名     
           p.StartInfo.UseShellExecute = false;            //关闭Shell的使用 
           p.StartInfo.RedirectStandardInput = true;       //重定向标准输入 
           p.StartInfo.RedirectStandardOutput = true;      //重定向标准输出 
           p.StartInfo.RedirectStandardError = true;       //重定向错误输出 
           p.StartInfo.CreateNoWindow = true;              //设置不显示窗口 
       }

       private void RunCmd(string command)
       {
           p.StartInfo.Arguments = "/c " + command;        //设定程序执行参数 
           p.Start();                                      //启动进程
           //p.StandardInput.WriteLine(command);           //也可以用这种方式输入要执行的命令 
           //p.StandardInput.WriteLine( "exit ");                 //不过要记得加上Exit要不然下一行程序执行的时候会进入死循环

           standardError = p.StandardError.ReadToEnd();
           standardOutput= p.StandardOutput.ReadToEnd();   //从出处流取得命令执行的结果 
           p.Close();
       }

       public bool Connect()
       {
           Disconnect();
           string command = @"net use \\" + hostName + @"\ipc$ " + passWord + "  /user:" + userName;
           //Console.WriteLine(command);
           RunCmd(command);
           //Console.WriteLine("connected");
           //command = "net view "+shareFolder;
           //RunCmd(command);
           if (standardError != "" && standardOutput == "")
           {
               command = @"net use \\" + hostName + @"\ipc$ ";
               RunCmd(command);
           }

           if (standardError != "" && standardOutput == "")
               return false;
           else 
               if (standardError == "" && standardOutput != "")
                 return true;
               else
                 return false;
       }
       public bool Disconnect()
       {
           string command = @"net use ";
           //string command = @"net use \\" + server + " /delete /y";
           RunCmd(command);
          if(standardError == "" && standardOutput != "")
           {
               string[] strs = standardOutput.Split(new string[]{"\r\n"},StringSplitOptions.RemoveEmptyEntries);
               for (int i = 0; i < strs.Length; i++)
               {
                   if (!strs[i].Contains(hostName)) continue;
                   command = strs[i].Substring(strs[i].IndexOf(hostName), strs[i].IndexOf(" ", strs[i].IndexOf(hostName)) - strs[i].IndexOf(hostName));
                   command = @"net use \\" + command + " /delete /y";
               }
           }
           //command = "net view " + shareFolder;
           RunCmd(command);
           if (standardError != "" && standardOutput == "")
               return true;
           else
               if (standardError == "" && standardOutput != "")
                   return false;
               else
                   return false;
       }
    }
}
